package com.ias.agdis_r.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.task.TaskSendOtherActivity;
import com.ias.agdis_r.bean.user.User;

/**
 * 成员列表ListView相关类
 * 
 * @author zhangsc
 * 
 */
public class MemberAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<User> items;
	private int type;
	private HashMap<String,Boolean> multiselectMap = new HashMap<String,Boolean>();

	public MemberAdapter(Context context, List<User> items,int type) {
		this.items = items;
		this.mInflater = LayoutInflater.from(context);
		this.type = type;
	}

	public void setList(List<User> items){

		this.items = items;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_member,
					null);
			holder.check = (CheckBox)convertView.findViewById(R.id.check_member);
			holder.seq = (TextView) convertView.findViewById(R.id.seq);
			holder.enm = (TextView) convertView.findViewById(R.id.enm);
			holder.enb = (TextView) convertView.findViewById(R.id.enb);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.seq.setText(String.valueOf(position + 1));
		holder.enm.setText((String) items.get(position).getEnm());
		holder.enb.setText((String) items.get(position).getEnb());

		String enb = items.get(position).getEnb();
		Boolean flag =  multiselectMap.get(enb);
		if(flag!=null&&flag){
			holder.check.setChecked(true);
		}else{
			holder.check.setChecked(false);
		}
		return convertView; 
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	public final class ViewHolder {
		public CheckBox check;
		public TextView seq;
		public TextView enm;
		public TextView enb;
	}
	
	public void check(int position,boolean flag){
		User user = items.get(position);
		multiselectMap.put(user.getEnb(),flag);
	}
	
	public List<String> getMultiselectList(){
		
		List<String> list = new ArrayList<String>();
		Iterator<Entry<String, Boolean>> iterator=  multiselectMap.entrySet().iterator();
		//获取选中项
		while(iterator.hasNext()){
			Entry<String, Boolean> entry = iterator.next();
			if(entry.getValue()){
				list.add(entry.getKey());		
			}
		}
		return list;
	}

	public void selectItem(int positon){
		User user = items.get(positon);
		String enb = user.getEnb();
		if(type==TaskSendOtherActivity.TYPE_TRANSFER) {
			multiselectMap.clear();
		}
		Boolean flag =  multiselectMap.get(enb);
		if(flag==null){
			flag = false;
		}
		multiselectMap.put(enb, !flag);
		this.notifyDataSetChanged();
	}
}
